package com.meng.listview_scrolldelete;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/5/19.
 */

public class MyAdapter extends BaseListViewAdapter<String>{
    public MyAdapter(Context context) {
        super(context);
    }
    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            notifyDataSetChanged();
        }
    };

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(getInflater().inflate(R.layout.item,null));
    }

    @Override
    protected void onBindViewHolder(ViewHolder holder, int position) {
        MyViewHolder mHolder = (MyViewHolder) holder;
        mHolder.messageTv.setText(getItem(position));
        mHolder.topTv.setTag(mHolder);
        mHolder.deleteTv.setTag(mHolder);
        mHolder.itemLayout.scrollTo(0,0);
    }

    private class MyViewHolder extends BaseListViewAdapter.ViewHolder{
        TextView messageTv;
        Button button;
        TextView topTv;
        TextView deleteTv;
        ItemLayout itemLayout;
        public MyViewHolder(View itemView) {
            super(itemView);
            itemLayout = (ItemLayout) itemView;
            messageTv = (TextView) itemView.findViewById(R.id.messageTv);
            topTv = (TextView) itemView.findViewById(R.id.topTv);
            deleteTv = (TextView) itemView.findViewById(R.id.deleteTv);
            button = (Button) itemView.findViewById(R.id.button);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(v == topTv){
                        if(onMenuClickListener != null){
                            onMenuClickListener.onTopClick(getPosition());
                            MyViewHolder myViewHolder = (MyViewHolder) v.getTag();
                            myViewHolder.itemLayout.closeMenu();
                        }
                    }else if(v == deleteTv){
                        if(onMenuClickListener != null){
                            MyViewHolder myViewHolder = (MyViewHolder) v.getTag();
                            onMenuClickListener.onDeleteClick(getPosition(),myViewHolder.itemLayout);
                        }
                    }else if(v == button){
                        Toast.makeText(v.getContext(), getItem(getPosition()), Toast.LENGTH_SHORT).show();
                    }
                }
            };
            topTv.setOnClickListener(clickListener);
            deleteTv.setOnClickListener(clickListener);
            button.setOnClickListener(clickListener);
        }
    }

    public interface OnMenuClickListener{
        void onTopClick(int position);
        void onDeleteClick(int position,ItemLayout itemLayout);
    }
    private OnMenuClickListener onMenuClickListener;
    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener){
        this.onMenuClickListener = onMenuClickListener;
    }

    public void toTop(int position){
        String remove = getDatas().remove(position);
        getDatas().add(0,remove);
        handler.sendEmptyMessageDelayed(0,300);
    }
    public void delete(int position,ItemLayout itemLayout) {
        getDatas().remove(position);
        notifyDataSetChanged();
    }
}
